package com.example.homemodule.view.qrcodeview;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.homemodule.R;
import com.uuzuche.lib_zxing.activity.CodeUtils;
import com.yilijk.base.utils.ALog;


import crossoverone.statuslib.StatusUtil;

public
class QrCodeActivity extends AppCompatActivity {

    private ImageView qrcodeBack;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StatusUtil.setSystemStatus(this, true, true);
        getWindow().setStatusBarColor(Color.WHITE);
        setContentView(R.layout.qrcode_layout);
        qrcodeBack = findViewById(R.id.qrcode_back);
        QrCodeFragment qrCodeFragment = new QrCodeFragment();
        qrCodeFragment.setAnalyzeCallback(analyzeCallback);
        getSupportFragmentManager().beginTransaction().replace(R.id.fl_qrcode_container, qrCodeFragment).commit();
        qrCodeFragment.setCameraInitCallBack(new QrCodeFragment.CameraInitCallBack() {
            @Override
            public void callBack(Exception e) {
                if (e == null) {

                } else {
                    ALog.e("TAG", "callBack: " + e.getMessage());
                }
            }
        });
        qrcodeBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                QrCodeActivity.this.finish();
            }
        });

    }

    /**
     * 二维码解析回调函数
     */
    CodeUtils.AnalyzeCallback analyzeCallback = new CodeUtils.AnalyzeCallback() {
        @Override
        public void onAnalyzeSuccess(Bitmap mBitmap, String result) {
            Intent resultIntent = new Intent();
            Bundle bundle = new Bundle();
            bundle.putInt("result_type", 1);
            bundle.putString("result_string", result);
            resultIntent.putExtras(bundle);
            QrCodeActivity.this.setResult(RESULT_OK, resultIntent);
            QrCodeActivity.this.finish();
        }

        @Override
        public void onAnalyzeFailed() {
            Intent resultIntent = new Intent();
            Bundle bundle = new Bundle();
            bundle.putInt("result_type", 2);
            bundle.putString("result_string", "");
            resultIntent.putExtras(bundle);
            QrCodeActivity.this.setResult(RESULT_OK, resultIntent);
            QrCodeActivity.this.finish();
        }
    };

}
